#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include"MagicTower.h"
#include"LoginSignup.h"
#include"StdFile.h"
int main(int argc, char const* argv[])
{
    srand(time(NULL));
    char* input = (char*)malloc(sizeof(char));
    char* input2 = (char*)malloc(sizeof(char));
    char* num = (char*)malloc(sizeof(char)); 
    char *ID = (char*)malloc(sizeof(char)*10);
    char *PWD = (char*)malloc(sizeof(char)*10);
    DBL GameUserData;

    if (input == NULL)
    {
        printf("input malloc is error!\n");
        input = NULL;
    }
    if (input2 == NULL)
    {
        printf("input2 malloc is error!\n");
        input2 = NULL;
    }
    char board[Map_Size][Map_Size];
    
    //初始化用户信息链表
    InitDBLinkList(&GameUserData);
    printf("*******************************************\n");
    printf("*             欢迎来到魔塔世界               *\n");
    printf("*******************************************\n");
    printf("----------------- 1.注册 --------------------\n");
    printf("----------------- 2.登录 --------------------\n");
    printf("请输入您的选择:");
    scanf("%c",num);
    //按下1注册
    if(strcmp(num,"1") == 0)
    {
        system("clear");
        printf("欢迎来到注册界面\n");
        printf("请输入注册账号:");
        scanf("%s",ID);
        printf("请输入注册密码:");
        scanf("%s",PWD);
        //注册
        SignUp(ID,PWD,"UserInformation.txt");
        printf("进入登陆界面请按2:");
        scanf("%s",num);
    }
    //按下2登录
    if(strcmp(num,"2") == 0)
    {
        system("clear");
        printf("欢迎来到登录界面\n");
        printf("请输入账号:");
        scanf("%s",ID);
        printf("请输入密码:");
        scanf("%s",PWD);
        //从文件中读取一行用户信息存入到链表中
        DBL *list = GetLineFromFile("UserInformation.txt");
        if(list == NULL)
        {
            printf("请先注册！\n");
            return 0;
        }
        //分割账户和密码存入GameUserData链表中
        DBTravel(list,StrTok,&GameUserData);
        //登录成功进入游戏
        if(Login(ID,PWD,&GameUserData) == true)
        {
            printf("按下i查看游戏介绍:");
            scanf("%s", input);
            if (strcmp(input, "i") == 0)
            {
                printf("--------------游戏介绍-------------------\n");
                printf("************1.W、A、S、D移动*************\n");
                printf("************2.M--打开商店****************\n");
                printf("************3.B--查看背包****************\n");
                printf("************4.Q--退出游戏****************\n");
                printf("************5.K--开始新游戏**************\n");
                printf("************6.C--继续上次游戏************\n");
            }
            printf("按下k开始新游戏------按下c继续上次游戏:");
            scanf("%s", input2);
            //按下k开始新游戏
            if ((strcmp(input2, "k") == 0) || (strcmp(input2, "K") == 0))
            {
                //初始化玩家信息，怪物信息，宝物信息
                
                P player;
                InitPlayer(&player);
                DmArr monsters = { NULL,10,0 };
                InitMonsters(&monsters);
                DmArr treasures = { NULL,10,0 };
                InitTreasure(&treasures);
                //初始化地图
                InitBoard(board,&monsters);
                //游戏的玩法实现
                Game(&treasures,&monsters,&player,board,ID,board);

                //释放
                for (int i = 0; i < monsters.len; i++)
                {
                    M* monster = (M*)monsters.dp[i];
                    free(monster->name.string);
                    free(monster);
                }
                for (int i = 0; i < treasures.len; i++)
                {
                    T* treasure = (T*)treasures.dp[i];
                    free(treasure->name.string);
                    free(treasure);
                }
                free(monsters.dp);
                free(treasures.dp);
            }
            //按下c继续上次游戏
            else if((strcmp(input2, "c") == 0) || (strcmp(input2, "C") == 0))
            {
                //初始化新的玩家信息，怪物信息，宝物信息，所以的信息都是从上次的存档中读取
                DBL list;
                InitDBLinkList(&list);
                char *UserTxt = (char *)malloc(sizeof(char) * 10);
                //根据玩家的账户名去匹配该玩家的存档文件
                strcpy(UserTxt,ID);
                strcat(UserTxt,".txt");
                P player;
                DmArr monsters = { NULL,10,0 };
                InitArray(&monsters);
                DmArr treasures = { NULL,10,0 };
                InitArray(&treasures);
                //初始化地图
                InitBoard(board,&monsters);
                //加载上一次的游戏存档
                LoadGameProcess(&monsters,&treasures,&player,UserTxt,&list);
                //玩法实现
                Game(&treasures,&monsters,&player,board,ID,board);
                //释放
                for (int i = 0; i < monsters.len; i++)
                {
                    M* monster = (M*)monsters.dp[i];
                    free(monster->name.string);
                    free(monster);
                }
                for (int i = 0; i < treasures.len; i++)
                {
                    T* treasure = (T*)treasures.dp[i];
                    free(treasure->name.string);
                    free(treasure);
                }
                free(monsters.dp);
                free(treasures.dp);
            }
        }
    } 
    free(input);
    free(input2);
    free(ID);
    free(PWD);
    free(num);
    FreeDBLList(&GameUserData);
    return 0;
}